package cn.tedu.mall.order.cosumer;

import cn.tedu.mall.front.service.IFrontSkuService;
import cn.tedu.mall.order.mapper.OrderMapper;
import cn.tedu.mall.pojo.order.model.OmsOrder;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RocketMQMessageListener(
        topic = "csmall-order-delay-topic",
        consumerGroup = "order-delay-group",
        selectorExpression = "*"
)
public class OrderDelayConsumer implements RocketMQListener<String> {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IFrontSkuService skuService;

    @Override
    public void onMessage(String message) {
        //messag就是订单编号,利用订单编号,查询订单是否存在OmsOrder
        OmsOrder order = orderMapper.selectOrderBySn(message);
        //判断订单状态是否state=0,
        if (order !=null && order.getState()==0){
            //面试题:订单库存是下单时候减,还是支付的时候减
            //如果是直接改成1
            //update oms_order set state=1 where sn=#{sn}
            orderMapper.shutDownOrderBySn(message);
            //回退库存
            skuService.returnStock(message);
        }
    }
}
